﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DataPreparation
{
    public class DocumentsReader
    {
        /// <summary>
        /// read all files from the directory
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> ReadAlltxtFile(string path)
        {
            Dictionary<string, string> m_allDocs = new Dictionary<string, string>();
            string[] files = Directory.GetFiles(path); // get all files from the path

            foreach (string file in files) // for each of the files
            {
                if (Path.GetExtension(file).CompareTo(".Mail") == 0)
                    return  ReadAllMailFile(path) ;
                string temp;
                List<string> cleanLine = new List<string>();
                string fileName = file.Split('\\').Last();
                StreamReader sr = new StreamReader(file); // open a new StreamReader
                if (fileName == "stopWords.txt") //if file: "stopWords.txt"
                    continue;
                temp = sr.ReadToEnd();
                m_allDocs.Add(fileName, temp); //adds all text from the file with key = Doc Name
                sr.Close();
            }
            return m_allDocs;
        }

        public static Dictionary<string, string> ReadAllMailFile(string path)
        {
            Dictionary<string, string> m_allDocs = new Dictionary<string, string>();
            string[] files = Directory.GetFiles(path); // get all files from the path

            foreach (string file in files) // for each of the files
            {
               
                //string[] recievers = null;
                //string [] time =null;
                //string tempString = "";
               // DateTime t;
               // string temp = "";
                string[] splitLine = null;
               // string[] splitLine2 = null;
              //  List<string> cleanLine = new List<string>();
                string fileName = file.Split('\\').Last();
                StreamReader sr = new StreamReader(file); // open a new StreamReader
                string firstLine = sr.ReadLine(); // Read a line 
                splitLine = firstLine.Split(new char[] { ',' }); // split the line and get the Doc Number
                    //recievers = splitLine[1].Split(new char[] { ';' });
                   // if (recievers[0] == "")
                       // continue;

                   // foreach (string i in recievers)
                   // {
                      //  tempString = "";
    
                               // if (i[0] == ' ')
                    //            {
                    //                tempString = i.Substring(1);
                    //                if (!cleanLine.Contains(tempString))
                    //                cleanLine.Add(tempString);
                    //            }
                    //            else
                    //                  if (!cleanLine.Contains(i))
                    //                cleanLine.Add(i);     
                    //}
                 //   splitLine2 = fileName.Split(new char[] { '#' }); // split the line and get the Doc Number
                 //   temp = splitLine2[1];
                 //   temp = temp.Remove(temp.Length - 4);
                  //  time = temp.Split(new char[] { '_' });
                  //  t = new DateTime(Convert.ToInt32(time[2]), Convert.ToInt32(time[0]), Convert.ToInt32(time[1]));
                  
                    //TMscore = Double.Parse(splitLine[2]);
                  //  List<string> rec = new List<string>(cleanLine);
         
                    m_allDocs.Add(fileName, sr.ReadToEnd()); //adds all text from the file with key = Doc Name
                    sr.Close();
                } 
            return m_allDocs;
        }

        public static void changeExtension()
        {
            string path = Directory.GetCurrentDirectory(); // get the directory where the EXE/Application file is
            path += "\\Mails"; // add the folder name "Files" to the path
            string[] files = Directory.GetFiles(path); // get all files from the path
            foreach (string file in files)
            {
                 
                  string extension = Path.GetExtension(file); 
                  //file.replace(extension,".Jpeg");
                  FileInfo f = new FileInfo(file);
                  f.MoveTo(Path.ChangeExtension(file, ".Mail"));
                    }
                }
    }
}
